extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

// 基础类数据
#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[wasm_bindgen]
pub fn get_greeting(name:&str) -> String {
    format!("hello, {}", name)
}

#[wasm_bindgen]
pub fn is_valid(num: u32) -> bool {
    num > 0
}

// 复杂的数据
#[wasm_bindgen]
// 它会自动为Person结构体实现Clone这个trait
#[derive(Clone)]
pub struct Person {
    name:String,
    age: i32,
}

#[wasm_bindgen]
impl Person {
    #[wasm_bindgen(constructor)]
    pub fn new(name: String, age: i32) -> Self {
        Person {name, age}
    }
    #[wasm_bindgen(method)]
    pub fn get_info(&self) -> String {
       format!("my name is {}, age is {}", self.name, self.age)
    }
}

